package com.lijj.activiti.demo.test;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.task.Task;

public class SimpleMultiInstance {
	@org.junit.Test
	public void test() {
		ProcessEngine engine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"temp/activiti.cfg.xml").buildProcessEngine();
		// ProcessEngineImpl engineImpl = (ProcessEngineImpl) engine;
		// 开户定时器
		// engineImpl.getProcessEngineConfiguration().getJobExecutor().start();
		RepositoryService repositoryService = engine.getRepositoryService();
		RuntimeService runtimeService = engine.getRuntimeService();
		TaskService taskService = engine.getTaskService();
		repositoryService.createDeployment()
				.addClasspathResource("bpmn/SimpleMultiInstance.bpmn").deploy();
		runtimeService.startProcessInstanceByKey("simpleMultiInstance");
		List<Execution> list = runtimeService.createExecutionQuery()
				.processDefinitionKey("simpleMultiInstance").list();
        
		System.out.println("使用顺序执行的多实例活动，流程数量："+list.size());
		for(int i=0;i<2;i++){
			Task task=taskService.createTaskQuery().processDefinitionKey("simpleMultiInstance").singleResult();
			System.out.println("任务id："+task.getId());
			taskService.complete(task.getId());
		}
		//流程到达第二个多实例活动（并行的）
        list=runtimeService.createExecutionQuery().processDefinitionKey("simpleMultiInstance").list();
        System.out.println("使用并行执行的多实例活动，流程数量："+list.size());
        for(int i=0;i<2;i++){
			Task task=taskService.createTaskQuery().processDefinitionKey("simpleMultiInstance").singleResult();
			System.out.println("任务id："+task.getId());
			taskService.complete(task.getId());
		}
	}
}
